<!-- 商家分布图表 -->
<template>
  <div class="com-container" @dblclick="revertMap">
    <div class="com-chart" ref="map_ref"></div>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import { getProvinceMapInfo } from "@/utils/map_utils";
import { mapData, chinaMap, provinceMap } from "@/api/index";
export default {
  data() {
    return {
      chartInstance: null,
      allData: null,
      mapData: {}, // 所获取的省份的地图矢量数据
    };
  },
  mounted() {
    this.initChart();
    this.getData();
    window.addEventListener("resize", this.screenAdapter);
    this.screenAdapter();
  },
  destroyed() {
    window.removeEventListener("resize", this.screenAdapter);
  },
  methods: {
    async initChart() {
      this.chartInstance = this.$echarts.init(this.$refs.map_ref, this.theme);
      try {
        let result = await chinaMap();
        this.$echarts.registerMap("china", result.data);
        const initOption = {
          title: {
            text: "▎商家分布",
            left: 20,
            top: 20,
          },
          geo: {
            type: "map",
            map: "china",
            top: "5%",
            bottom: "5%",
            itemStyle: {
              areaColor: "#2E72BF",
              borderColor: "#333",
            },
          },
          legend: {
            left: "5%",
            bottom: "5%",
            orient: "vertical",
          },
        };
        this.chartInstance.setOption(initOption);
      } catch (e) {
        console.log(e);
      }
      //获取中国地图的矢量数据
      this.chartInstance.on("click", async (event) => {
        try {
          const provinceInfo = getProvinceMapInfo(event.name);
          //查看是否已经有了缓存数据
          if (!this.mapData[provinceInfo.key]) {
            let result = await provinceMap(provinceInfo);
            this.$echarts.registerMap(provinceInfo.key, result.data);
            //缓存地图
            this.mapData[provinceInfo.key] = result.data;
          }
          const changeOption = {
            geo: {
              map: provinceInfo.key,
            },
          };
          this.chartInstance.setOption(changeOption);
        } catch (e) { }
      });
    },
    async getData() {
      // 获取服务器的数据, 对this.allData进行赋值之后, 调用updateChart方法更新图表
      try {
        let result = await mapData();
        this.allData = result;
        this.updateChart();
      } catch (e) {
        console.log(e);
      }
    },
    updateChart() {
      //图例的数据
      const legendArr = this.allData.map((item) => {
        return item.name;
      });
      //处理图标需要的数据
      const seriesArr = this.allData.map((item) => {
        //一个类别下的所有的散点数据
        return {
          type: "effectScatter",
          rippleEffect: {
            scale: 5, //涟漪效果
            brushType: "stroke", //空心涟漪效果
          },
          name: item.name,
          data: item.children,
          coordinateSystem: "geo", //给散点的图标增加一个配置,coordinateSystem:geo
        };
      });
      const dataOption = {
        legend: {
          data: legendArr,
        },
        series: seriesArr,
      };
      this.chartInstance.setOption(dataOption);
    },
    screenAdapter() {
      const titleFontSize = (this.$refs.map_ref.offsetWidth / 100) * 3.6;
      const adapterOption = {
        title: {
          textStyle: {
            fontSize: titleFontSize,
          },
        },
        legend: {
          itemWidth: titleFontSize / 2,
          itemHeight: titleFontSize / 2,
          itemGap: titleFontSize / 2,
          textStyle: {
            fontSize: titleFontSize,
          },
        },
      };
      this.chartInstance.setOption(adapterOption);
      this.chartInstance.resize();
    },
    //双击回到中国地图
    revertMap() {
      const revertOption = {
        geo: {
          map: "china",
        },
      };
      this.chartInstance.setOption(revertOption);
    },
  },
  computed: {
    ...mapState(['theme'])
  },
  watch: {
    theme() {
      this.chartInstance.dispose()//销毁当前的图标
      this.initChart()//重新初始化图标
      this.screenAdapter()//完成屏幕适配
      this.updateChart()//更新图标展示
    }
  }
};
</script>

<style lang='less' scoped>

</style>
